package com.peng.sell.controller;

import com.peng.sell.dataobject.ProductCategory;
import com.peng.sell.dataobject.ProductInfo;
import com.peng.sell.dto.OrderDTO;
import com.peng.sell.enums.ResponseCodeEnum;
import com.peng.sell.exception.SellException;
import com.peng.sell.form.ProductForm;
import com.peng.sell.service.OrderService;
import com.peng.sell.service.ProductCategoryService;
import com.peng.sell.service.ProductInfoService;
import com.peng.sell.utils.PageUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import sun.security.util.KeyUtil;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/seller/product")
@Slf4j
public class SellerProductController {
    @Autowired
    private ProductInfoService productInfoService;
    @Autowired
    private ProductCategoryService categoryService;

    @GetMapping("/list")
    public String list(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                       @RequestParam(value = "pageSize", defaultValue = "2") Integer pageSize,
                       Map<String, Object> dataMap) {
        PageRequest pageRequest = new PageRequest(pageNum - 1, pageSize);
        Page<ProductInfo> data = productInfoService.findAll(pageRequest);
        dataMap.put("data", data);
        dataMap.put("currentPage",pageNum);
        dataMap.put("pageSize",pageNum);
        dataMap.put("pagination",PageUtil.getPagination(pageNum,data.getTotalPages()));

        return "/product/list";
    }

    /**
     * 商品上架
     * @param productId
     * @param map
     * @return
     */
    @GetMapping("/on_sale")
    public String  onSale(String productId,Map<String,Object> map){
        try {
            productInfoService.onSale(productId);
        }catch (SellException e){
            log.error("【商品上架】出现异常：e：{}",e);
            map.put("msg",e.getMessage());
            map.put("url","/sell/seller/product/list");
            return "/common/error";
        }
        map.put("msg",ResponseCodeEnum.PRODUCT_ON_SALE_SUCCESS.getDesc());
        map.put("url","/sell/seller/product/list");
        return "/common/success";
    }

    /**
     * 商品下架
     * @param productId
     * @param map
     * @return
     */
    @GetMapping("/off_sale")
    public String  offSale(String productId,Map<String,Object> map){
        try {
            productInfoService.offSale(productId);
        }catch (SellException e){
            log.error("【商品下架】出现异常：e：{}",e);
            map.put("msg",e.getMessage());
            map.put("url","/sell/seller/product/list");
            return "/common/error";
        }

        return "/common/success";

    }

    @GetMapping("/index")
    public String index(@RequestParam(value ="productId",required = false) String productId,Map<String,Object> map){
        if(!StringUtils.isEmpty(productId)){
            ProductInfo productInfo = productInfoService.findOne(productId);
            map.put("product",productInfo);
        }
        List<ProductCategory> categoryList = categoryService.findAll();
        map.put("categoryList",categoryList);
        return "/product/index";
    }

    @PostMapping("/save")
    public String save(@Validated ProductForm productForm, BindingResult bindingResult, Map<String, Object> map){
        if(bindingResult.hasErrors()){
            map.put("msg",bindingResult.getFieldError().getDefaultMessage());
            map.put("url","/sell/seller/product/index");
            return "/common/error";
        }
        ProductInfo productInfo = new ProductInfo();
        if(!StringUtils.isEmpty(productForm.getProductId())){
            productInfo = productInfoService.findOne(productForm.getProductId());
        }else{
            productForm.setProductId(com.peng.sell.utils.KeyUtil.getUUID());
        }

        BeanUtils.copyProperties(productForm,productInfo);
        productInfoService.save(productInfo);
        map.put("url","/sell/seller/product/list");
        return "/common/success";
    }
}
